﻿using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// Helper class for Pagnation
/// </summary>

namespace si.caha.Helper
{
    public class Pagnation
    {
        public int ItemsPerPage { get; set; }
        public int CurrentPageNumber { get; set; }
        public int ItemsCount { get; set; }

        public Pagnation(int itemsPerPage)
        {
            ItemsPerPage = itemsPerPage;
            CurrentPageNumber = 1;  // first page
        }

        /**
         * Example:
         * 20 items, 10 items per page => 2
         * 21 items, 10 items per page => 3
         */
        public int LastPage()
        {
            return (ItemsCount % ItemsPerPage == 0) ? ItemsCount / ItemsPerPage : (ItemsCount / ItemsPerPage) + 1;
        }

        public string LastPageLink()
        {
            return (NextPageLink().Length > 0) ? ">" + NextPageLink() : "";
        }

        public string FirstPageLink()
        {
            return (PreviousPageLink().Length > 0) ? "<" + PreviousPageLink() : "";
        }

        public string PreviousPageLink()
        {
            if (CurrentPageNumber == 1)
                return "";

            return "<";
        }

        /**
         * Example:
         * ItemsCount = 7
         * CurrentPageNumber = 3
         * ItemsPerPage = 3
         * 3 * 3 >= 7
         * 
         */
        public string NextPageLink()
        {
            if (CurrentPageNumber * ItemsPerPage >= ItemsCount)
                return "";

            return ">";
        }

        public string GetFirstPageNumberUrl(String currentUrl)
        {
            return BuildPageNumberUrl(currentUrl, 1, 0);
        }

        public string GetLastPageNumberUrl(String currentUrl)
        {
            return BuildPageNumberUrl(currentUrl, LastPage(), 0);
        }

        public string GetNextPageNumberUrl(String currentUrl)
        {
            return BuildPageNumberUrl(currentUrl, CurrentPageNumber, 1);
        }

        public string GetPreviousPageNumberUrl(String currentUrl)
        {
            return BuildPageNumberUrl(currentUrl, CurrentPageNumber, -1);
        }

        private string BuildPageNumberUrl(String currentUrl, int pageNumber, int currentPageNumberAddition)
        {
            string result = RemoveFilter(currentUrl, CurrentPageNumber);

            if (result.Contains("?"))
                result += "&pageNumber=" + (pageNumber + currentPageNumberAddition);
            else
                result += "?pageNumber=" + (pageNumber + currentPageNumberAddition);

            return result;
        }

        public static string RemoveFilter(string currentUrl, int currentPageNumber)
        {
            string result = currentUrl;
            result = result.Replace("&pageNumber=" + currentPageNumber, "");
            result = result.Replace("?pageNumber=" + currentPageNumber, "");
            return result;
        }
    }
}